package inet.app.vod.activity.account;

import inet.app.vod.activity.R;
import inet.app.vod.activity.common.BaseActivity;
import inet.app.vod.net.http.VolleyClient;
import inet.app.vod.net.http.VolleyListener;
import inet.app.vod.object.ResultJsonModel;
import inet.app.vod.object.User;
import inet.app.vod.parser.Parse;
import inet.app.vod.util.MyPreferenceManager;
import inet.app.vod.util.MyUtil;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;

import com.android.volley.VolleyError;
import com.inet.app.framework.networkutil.NetworkUtil;
import com.inet.app.framework.util.Log;
import com.inet.app.framework.util.hardware.KeyboardUtil;
import com.inet.app.framework.util.text.ValidateUtil;
import com.inet.app.framework.widget.MyToast;

public class RegisterActivity extends BaseActivity {

	private EditText mUsernameEdt;
	private EditText mPassEdt;
	private EditText mRetypePassEdt;
	private EditText mEmailEdt;
	private View mErrorView;

	private String mUserName;
	private String mPass;
	private String mEmail;

	View.OnClickListener mRegisterListener = new View.OnClickListener() {

		@Override
		public void onClick(View v) {

			boolean cancel = validateField();
			if (cancel) {
				mErrorView.requestFocus();
				return;
			} else {
				KeyboardUtil.hideSoftKeyboard(RegisterActivity.this);
			}

			// Check network
			if (!NetworkUtil.getInstance(getApplicationContext()).isConnect()) {
				MyUtil.showDialog(getString(R.string.error_connect),
						RegisterActivity.this);
				return;
			}
			implementRegister();
		}
	};

	/**
	 * Implement after check valid and network
	 */
	public void implementRegister() {

		VolleyClient.register(mUserName, mPass, mEmail,
				new VolleyListener(this) {

					@Override
					public void onSuccess(String response) {
						Log.d("success request" + response);
						ResultJsonModel jResult = Parse.parseAccount(response);
						if (jResult == null) {
							// error parser
							MyUtil.showDialog(getString(R.string.loi_dang_ki),
									RegisterActivity.this);
						} else if (jResult.hasError()) {
							// error register
							String error = jResult.getError();
							MyUtil.showDialog(error, RegisterActivity.this);
						} else {
							// register success
							MyToast.show(RegisterActivity.this,
									getString(R.string.dang_ki_thanh_cong));
							// Save account to preference
							User acc = (User) jResult.getResult();
							acc.setPassword(mPass);
							MyPreferenceManager.getInstance(
									RegisterActivity.this).setAccount(acc);
							finish();
						}
					}

					@Override
					public void onStart() {
						showLoading(true);
					}

					@Override
					public void onFinish() {
						showLoading(false);
					}

					@Override
					public void onError(VolleyError error) {
						MyUtil.showDialog(getString(R.string.loi_dang_ki)
								+ " (" + error.getMessage() + ")",
								RegisterActivity.this);
					}
				});

	}

	private void initViews() {
		mUsernameEdt = (EditText) findViewById(R.id.username_edt);
		mPassEdt = (EditText) findViewById(R.id.newpass_edt);
		mRetypePassEdt = (EditText) findViewById(R.id.retype_newpass_edt);
		mEmailEdt = (EditText) findViewById(R.id.email_edt);
		findViewById(R.id.signin_btn).setOnClickListener(mRegisterListener);
		showRightButton(false);
	}

	/**
	 * Check valid all fields in edit text
	 * 
	 * @return
	 */
	private boolean validateField() {

		/* Reset errors */
		mUsernameEdt.setError(null);
		mPassEdt.setError(null);
		mRetypePassEdt.setError(null);
		mEmailEdt.setError(null);

		mUserName = mUsernameEdt.getText().toString();
		mPass = mPassEdt.getText().toString();
		String retypePass = mRetypePassEdt.getText().toString();
		mEmail = mEmailEdt.getText().toString();

		boolean cancel = false;

		/* Check valid username */
		if (TextUtils.isEmpty(mUserName)) {
			mUsernameEdt
					.setError(getString(R.string.ten_dang_nhap_khong_duoc_de_trong));
			mErrorView = mUsernameEdt;
			cancel = true;
			return cancel;
		} else if (!ValidateUtil.validateUserName(mUserName)) {
			mUsernameEdt
					.setError(getString(R.string.ten_dang_nhap_khong_hop_le));
			mErrorView = mUsernameEdt;
			cancel = true;
			return cancel;
		}

		/* Check valid password */
		if (TextUtils.isEmpty(mPass) || mPass.length() < 6) {
			mPassEdt.setError(getString(R.string.mat_khau_it_nhat_6_ki_tu));
			mErrorView = mPassEdt;
			cancel = true;
			return cancel;
		}

		/* Check valid retype password */
		if (!retypePass.equals(mPass)) {
			mRetypePassEdt
					.setError(getString(R.string.mat_khau_khong_trung_nhau));
			mErrorView = mRetypePassEdt;
			cancel = true;
			return cancel;
		}

		/* Check valid email */
		if (TextUtils.isEmpty(mEmail)) {
			mEmailEdt.setError(getString(R.string.email_khong_duoc_de_trong));
			mErrorView = mEmailEdt;
			cancel = true;
			return cancel;
		} else if (!ValidateUtil.validateEmail(mEmail)) {
			mEmailEdt.setError(getString(R.string.email_khong_hop_le));
			mErrorView = mEmailEdt;
			cancel = true;
			return cancel;
		}
		return cancel;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_signin);
		initUIBase();
		initViews();
		setTitle(getResources().getString(R.string.dang_ki));
	}

	@Override
	public void setTagMenu() {
		mTagMenu = -1;
	}

}
